package com.example.casper.Experiment2024.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.fragment.BookListFragment;
import com.example.casper.Experiment2024.fragment.ClockViewFragment;
import com.example.casper.Experiment2024.fragment.GameViewFragment;
import com.example.casper.Experiment2024.fragment.TencentMapsFragment;
import com.example.casper.Experiment2024.fragment.WebViewFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
// hello world
public class Hello2249Activity extends AppCompatActivity {

    private ViewPager2 viewPager; // ViewPager2控件，用于滑动显示不同的Fragment
    private TabLayout tabLayout; // TabLayout控件，用于显示选项卡

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello2249); // 设置布局文件

        // 初始化TabLayout和ViewPager2控件
        tabLayout = findViewById(R.id.tab_layout); // 获取TabLayout
        viewPager = findViewById(R.id.view_pager); // 获取ViewPager2

        // 创建ViewPager适配器并设置到ViewPager2
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        viewPager.setAdapter(adapter);

        // 将TabLayout和ViewPager2关联，设置Tab标题
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> {
                    switch (position) {
                        case 0:
                            tab.setText("Books"); // 设置第一个Tab标题
                            break;
                        case 1:
                            tab.setText("Search"); // 设置第二个Tab标题
                            break;
                        case 2:
                            tab.setText("Map"); // 设置第三个Tab标题
                            break;
                        case 3:
                            tab.setText("Clock"); // 设置第四个Tab标题
                            break;
                        case 4:
                            tab.setText("Dodging Bombs Game"); // 设置第五个Tab标题
                            break;
                    }
                }).attach(); // 将TabLayout和ViewPager2绑定并显示
    }

    // ViewPager适配器，管理每个页面显示的Fragment
    private static class ViewPagerAdapter extends FragmentStateAdapter {
        public ViewPagerAdapter(FragmentActivity fa) {
            super(fa); // 调用父类构造方法，传入FragmentActivity
        }

        // 根据位置创建相应的Fragment
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new BookListFragment(); // 返回书籍列表Fragment
                case 1:
                    return new WebViewFragment(); // 返回WebViewFragment
                case 2:
                    return new TencentMapsFragment(); // 返回腾讯地图Fragment
                case 3:
                    return new ClockViewFragment(); // 返回时钟Fragment
                case 4:
                    return new GameViewFragment(); // 返回游戏Fragment
                default:
                    return new BookListFragment(); // 默认返回书籍列表Fragment
            }
        }

        // 返回页面数量，这里有5个页面
        @Override
        public int getItemCount() {
            return 5; // 页面数量为5
        }
    }
}
